# encoding: utf-8
class MapController < ApplicationController
  skip_before_filter :verify_authenticity_token, :only => [:wms, :wfs]
  before_filter :login_required
  
  # map_controller method that shows the main map interface.
  #
  # Also serves as the root_path of the app.
  #
  def index
  end

  # map_controller method that handles WMS queries.
  #
  # If the query is HTTP GET the method extracts the query string
  # from the string and passes it to the wmsGet model method.
  #
  # If the query is HTTP POST the method passes the raw post data
  # to the wmsPost model method.
  #
  # The response is rendered as is with 'render :text'.
  #
  def wms
    query =  request.fullpath.gsub(/\A\/map\/wms\?/, '')
    if request.post?
      res = Map.wmsPost(session[:user][:username], session[:user][:password], request.raw_post)
    else
      res = Map.wmsGet(session[:user][:username], session[:user][:password], query)
    end
    render :text => res.body, :layout => false, :content_type => res.headers['content-type'], :status => res.code
  end

  # map_controller method that handles WFS queries.
  #
  # If the query is HTTP GET the method extracts the query string
  # from the string and passes it to the wfsGet model method.
  #
  # If the query is HTTP POST the method passes the raw post data
  # to the wfsPost model method.
  #
  # The response is rendered as is with 'render :text'.
  #
  def wfs
    query =  request.fullpath.gsub(/\A\/map\/wfs\?/, '')
    if request.post?
      res = Map.wfsPost(session[:user][:username], session[:user][:password], request.raw_post)
    else
      res = Map.wfsGet(session[:user][:username], session[:user][:password], query)
    end
    render :text => res.body, :layout => false, :content_type => res.headers['content-type'], :status => res.code
  end
end
